import 'reflect-metadata';
import fastify from 'fastify'
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'
import { Container } from 'inversify';
import { buildProviderModule } from 'inversify-binding-decorators'

import sequelize from './config/database';
import UserRoutes from './routes/user.routes';

const app = fastify({
  logger: true
}).withTypeProvider<TypeBoxTypeProvider>()

const container = new Container();
// 自动绑定所有@provide装饰的模块到容器
container.load(buildProviderModule());

app.register(async (instance) => {
  await sequelize.sync({ alter: true });
  instance.log.info('Database synchronized');
});

// Example route with TypeScript types
app.get<{
  Querystring: { name?: string }
}>('/', async (request, reply) => {
  return { hello: request.query.name || 'world' }
});
// 注册路由
// app.register(import('./routes/user.routes'), { prefix: '/users' });
const userRoutes = container.get<UserRoutes>(UserRoutes);
app.register(async (fastify) => {
  await userRoutes.register(fastify);
}, { prefix: '/users' });

export default app;
